#Fichier gérant l'affichage de l'onglet correspondant au bureau, permettant à l'utilisateur de rechercher ou modifier un membre en appelant des fonctions de gestion de la BD
use utf8;
   
############################################################################################
#                             Définition de l'onglet Bureau                                #
############################################################################################

# Onglet Bureau
my $bureau = $blocnote->add('Bureau', -label => 'Bureau',);
$bureau->configure( -background => 'white',);

#Mise en place du titre
my $c = $bureau->Canvas(-width => 500, -height => 1, -background => 'white', -borderwidth => 0)->pack(-pady => 2);
$c->createLine(1,1, 500,1);
$bureau->Label ( -text => "Gérer les membres du bureau", -background => 'white')->pack();
$c = $bureau->Canvas(-width => 500, -height => 1, -background => 'white', -borderwidth => 0)->pack();
$c->createLine(1,1, 500,1);

# Définition des champs de recherche
my $fieldBureau = $bureau->Frame(-background => 'white')->pack(-anchor => 'n', -pady => 15, -padx => 15);

$fieldBureau->Label( -text => 'Nom:', -background => 'white', -justify => 'left')->grid(-row => 0, -column => 0, -sticky => 'w');
my $name = $fieldBureau->Entry()->grid(-row => 0, -column => 1, -sticky => 'w');

$fieldBureau->Label(-text => 'Prénom:', -background => 'white')->grid(-row => 1, -column => 0, -sticky => 'w');
my $firstName = $fieldBureau->Entry()->grid(-row => 1, -column => 1, -sticky => 'w');

$fieldBureau->Label(-text => 'Login:', -background => 'white')->grid(-row => 2, -column => 0, -sticky => 'w');
my $login = $fieldBureau->Entry()->grid(-row => 2, -column => 1, -sticky => 'w');

$fieldBureau->Label( -text => 'Filière:', -background => 'white',)->grid(-row => 3, -column => 0, -sticky => 'w',);
my $filiere = '';
$fieldBureau->BrowseEntry(-listheight => 4, -choices => ['Informatique', 'Télécommunication', 'Électronique', 'Matmeca'], -variable => \$filiere)->grid(-row => 3, -column => 1, -sticky => 'w');

$fieldBureau->Label( -text => 'Promotion:', -background => 'white',)->grid(-row => 4, -column => 0, -sticky => 'w');
my $promotion = $fieldBureau->Entry()->grid(-row => 4, -column => 1, -sticky => 'w');

$fieldBureau->Label( -text => 'Date:', -background => 'white',)->grid(-row => 5, -column => 0, -sticky => 'w');
my $dateSubscription = $fieldBureau->Entry()->grid(-row => 5, -column => 1, -sticky => 'w');

my $buttonFrame = $bureau->Frame(-background => 'white')->pack(-anchor => 'n');

$buttonFrame->Button(-text => 'Ajouter adhérent', -command => [\&AddBureauMember])->grid(-row => 6, -column => 0, -pady => 5, -sticky => 'w', -padx => 7);
$buttonFrame->Button(-text => 'Rechercher', -command => [\&SearchBureauMembers])->grid(-row => 6, -column => 1, -pady => 5, -sticky => 'w', -padx => 7);
my $foo = 42;
$buttonFrame->Button(-text => 'Effacer', -command =>[\&ClearSubscriber,$name,$firstName,$login,$promotion,$dateSubscription, \$filiere, \$foo])->grid(-row => 6, -column => 3, -pady => 5, -sticky => 'w', -padx => 7);

############################################################################################
#                        Fonctions de gestion des membres du Bureau                        #
############################################################################################

#Fonction affichant le résultat de la recherche d'un membre du bureau
sub SearchBureauMembers {
  my $i = 1;
  my $dialog_box = $window->DialogBox(-title => 'Résultat de la recherche', -buttons => ["Supprimer", "Modifier", "Quitter"], -background => 'white');
 
  $dialog_box->Label(-text => "Nom", -relief =>'groove',)->grid(-row => 0, -column => 1, -sticky => "nsew");
  $dialog_box->Label(-text => "Prénom", -relief =>'groove',)->grid(-row => 0, -column => 2, -sticky => "nsew");
  $dialog_box->Label(-text => "Login", -relief =>'groove',)->grid(-row => 0, -column => 3, -sticky => "nsew");
  $dialog_box->Label(-text => "Filière", -relief =>'groove',)->grid(-row => 0, -column => 4, -sticky => "nsew");
  $dialog_box->Label(-text => "Promotion", -relief =>'groove',)->grid(-row => 0, -column => 5, -sticky => "nsew");
  $dialog_box->Label(-text => "Date d'entrée", -relief =>'groove',)->grid(-row => 0, -column => 6, -sticky => "nsew");
  $dialog_box->Label(-text => "Date de sortie", -relief =>'groove',)->grid(-row => 0, -column => 7, -sticky => "nsew");
  
  my $membersList = gestion_bd::searchBureauMembers($name->get, $firstName->get, $promotion->get, $filiere, $login->get, $dateSubscription->get);
  my $member;

  foreach my $subscribers (sort {$membersList->{$b}{dateSortie} cmp $membersList->{$a}{dateSortie} } keys %{$membersList}) {
    $dialog_box->Radiobutton(-value=>$membersList->{$subscribers}{idBureau}, -variable=>\$member)->grid(-row => $i, -column => 0,);
    $dialog_box->Label(-text => $membersList->{$subscribers}{nomPersonne}, -background => 'white')->grid(-row => $i, -column => 1,);
    $dialog_box->Label(-text => $membersList->{$subscribers}{prenomPersonne}, -background => 'white')->grid(-row => $i, -column => 2,);
    $dialog_box->Label(-text => $membersList->{$subscribers}{login}, -background => 'white')->grid(-row => $i, -column => 3,);
    $dialog_box->Label(-text => $membersList->{$subscribers}{filiere}, -background => 'white')->grid(-row => $i, -column => 4,);
    $dialog_box->Label(-text => $membersList->{$subscribers}{promotion}, -background => 'white')->grid(-row => $i, -column => 5,);
    $dialog_box->Label(-text => gestion_bd::convertDate($membersList->{$subscribers}{dateEntree}), -background => 'white')->grid(-row => $i, -column => 6,);

    if($membersList->{$subscribers}{dateSortie} ne "9999-12-31") { 
      $dialog_box->Label(-text => gestion_bd::convertDate($membersList->{$subscribers}{dateSortie}), -background => 'white')->grid(-row => $i, -column => 7,);
    }

    $i = $i + 1;
  }
 
  my $response = $dialog_box->Show();

  if ($response eq "Modifier" and defined($member)) {
    ModifyBureauMember($member);
  }
  elsif ($response eq "Supprimer" and defined($member)) {
    gestion_bd::deleteBureauMember($member);
    dialogBox("Information", "Membre du bureau supprimé");
  }
  elsif($response eq "Supprimer" or $response eq "Modifier") {
    errorBox("Veuillez sélectionner un adhérent");
    SearchBureauMembers();
  }
}

#Fonction permettant d'ajouter un adhérent au Bureau
sub AddBureauMember {
  my $i = 1;
  my $dialog_box = $window->DialogBox(-title  => 'Adhérents non membre du Bureau', -buttons => ["Ajouter", "Quitter"], -background => 'white');

  $dialog_box->Label(-text => "Nom", -relief =>'groove',)->grid(-row => 0, -column => 1, -sticky => "nsew");
  $dialog_box->Label(-text => "Prénom", -relief =>'groove',)->grid(-row => 0, -column => 2, -sticky => "nsew");
  $dialog_box->Label(-text => "Login", -relief =>'groove',)->grid(-row => 0, -column => 3, -sticky => "nsew");
  $dialog_box->Label(-text => "Filière", -relief =>'groove',)->grid(-row => 0, -column => 4, -sticky => "nsew");
  $dialog_box->Label(-text => "Promotion", -relief =>'groove',)->grid(-row => 0, -column => 5, -sticky => "nsew");
  $dialog_box->Label(-text => "Date d'adhésion", -relief =>'groove',)->grid(-row => 0, -column => 6, -sticky => "nsew");

  my $membersList = gestion_bd::searchMember($name->get, $firstName->get, $promotion->get, $filiere, $login->get, $dateSubscription->get, 2);
  my $member;

  my @membersHash = keys %{$membersList};
  foreach my $subscribers (@membersHash) {
    $dialog_box->Radiobutton(-value=>$membersList->{$subscribers}{login}, -variable=>\$member)->grid(-row => $i, -column => 0,);
    $dialog_box->Label(-text => $membersList->{$subscribers}{nomPersonne}, -background => 'white')->grid(-row => $i, -column => 1,);
    $dialog_box->Label(-text => $membersList->{$subscribers}{prenomPersonne}, -background => 'white')->grid(-row => $i, -column => 2,);
    $dialog_box->Label(-text => $membersList->{$subscribers}{login}, -background => 'white')->grid(-row => $i, -column => 3,);
    $dialog_box->Label(-text => $membersList->{$subscribers}{filiere}, -background => 'white')->grid(-row => $i, -column => 4,);
    $dialog_box->Label(-text => $membersList->{$subscribers}{promotion}, -background => 'white')->grid(-row => $i, -column => 5,);
    $dialog_box->Label(-text => gestion_bd::convertDate($membersList->{$subscribers}{dateAdhesion}), -background => 'white')->grid(-row => $i, -column => 6,);
    $i = $i + 1;
  }

  my $response = $dialog_box->Show(); 

  if ($response eq "Ajouter" and defined($member)) {
    my $dateDialogBox = $dialog_box->DialogBox(-title => "Date d'entrée en fonction", -buttons => ["OK", "Annuler"], -background => "white");
    $dateDialogBox->Label(-text => "Date d'entrée en fonction", -background => "white")->grid(-column => 0, -row => 0);
    my $date = $dateDialogBox->Entry()->grid(-column => 1, -row => 0,);
    if ($dateDialogBox->Show() eq "OK") {
      gestion_bd::addBureauMember($membersList->{$member}{nomPersonne},$membersList->{$member}{prenomPersonne},$membersList->{$member}{promotion}, $membersList->{$member}{filiere},$membersList->{$member}{login}, $membersList->{$member}{dateAdhesion}, $date->get);
    }
  } 
  elsif ($response eq "Ajouter") {
    dialogBox("Erreur", "Veuillez sélectionner un adhérent");
    AddBureauMember();
  }
}

#Fonction modifiant les informations concernant un membre du Bureau (idBureau)
sub ModifyBureauMember {
  my ($dateEntree, $dateSortie) = gestion_bd::quickSearchBureauMember($_[0]);

  if($dateSortie eq "9999-12-31") {
    $dateSortie = "";
  }

  $dateEntree = gestion_bd::convertDate($dateEntree);
  $dateSortie = gestion_bd::convertDate($dateSortie);

  my $dialog_box = $window->DialogBox(-title => "Modification du membre du bureau", -buttons => ["OK", "Annuler"], -background => 'white', -default_button => 'Annuler');

  $dialog_box->Label(-text => "Date d'entrée", -background => "white")->grid(-row => 0, -column => 0);
  $dialog_box->Entry(-textvariable => \$dateEntree)->grid(-row => 0, -column => 1);
  $dialog_box->Label(-text => "Date de sortie", -background => "white")->grid(-row => 1, -column => 0);
  $dialog_box->Entry(-textvariable => \$dateSortie)->grid(-row => 1, -column => 1);

  my $response = $dialog_box->Show();
  
  if ($dateSortie eq "") {
    $dateSortie = "31/12/9999";
  }
  if ($response eq "OK" and gestion_bd::convertDate($dateSortie) lt gestion_bd::convertDate($dateEntree)) {
    dialogBox("Erreur", "La date de sortie ne peut être inférieure à la date d'entrée");
    ModifyBureauMember($_[0]);

  }
  elsif ($response eq "OK") {
   
    gestion_bd::updateBureauMember($_[0], $dateEntree, $dateSortie);
    dialogBox("Information", "Mise à jour effectuée");
  }
}
